//
//  TestRecordVC.swift
//  Auscultation
//
//  Created by 张文博 on 2021/4/4.
//

import UIKit
import HandyJSON
import SwifterSwift

class TestRecordVC: BaseVC {
    var pageNum: Int = 1
    var pageSize: Int = 10
    var dataSource: [TestRecordModel?] = []
    lazy var table: UITableView = {
        let table = UITableView(frame: .zero, style: .grouped)
        view.addSubview(table)
        table.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-UIDevice.lgf_BottomSafeAreaH())
            make.top.equalToSuperview().offset(UIDevice.lgf_NavBarH())
        }
        table.delegate = self
        table.dataSource = self
        table.register(nib: UINib.init(nibName: "TestRecordTableViewCell", bundle: Bundle.main), withCellClass: TestRecordTableViewCell.self)
        table.separatorStyle = .none
        table.backgroundColor = .clear
        return table
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        let top_view = UIImageView()
        top_view.image = UIImage(named: "table2")
        top_view.isUserInteractionEnabled = true
        self.view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_NavBarH())
        }
        
        self.navTitle = "测评结果"
        let style = LGFNavigationBarStyle.lgf()
        style.lgf_LeftBtnImageLight = UIImage(named: "darkback")!

        self.lgf_NavigationBar.style.lgf_RightBtnTitleColor = UIColor.init(lgf_HexString: "FFFFFF")
        self.lgf_NavigationBar.style.lgf_TitleColor = UIColor.init(lgf_HexString: "FFFFFF")
        self.lgf_NavigationBar.lgf_line?.isHidden = true
        self.lgf_NavigationBar.lgf_Reload()


        table.mj_header = MJRefreshNormalHeader.init(refreshingBlock: { [weak self] in
            self?.pageNum = 1
            self?.loadData()

        })
        
        table.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
            [weak self] in
            self?.pageNum += 1
            self?.loadData()

        })

        loadData()
    }
}


extension TestRecordVC : UITableViewDelegate, UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withClass: TestRecordTableViewCell.self)
        guard let model = dataSource[indexPath.row] else { return cell }
        cell.model = model
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

        return CGFloat.leastNormalMagnitude
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer = UIView()
        footer.backgroundColor = .white
        return footer
    }
}


extension TestRecordVC {
    func loadData() {
        let paramet  = [
            "pageNo" : pageNum.description,
            "pageSize" : pageSize.description,
            "userId" : EWEUser.lgf.userId ?? "",
            "encryptedData": "zFoamlSX95gBI7S05+e6HakuGltzhbckTNdkY0yZ2hU=",
        ]
        EWERequest.lgf("exam/user/" + EWEUser.lgf.userId , paramet, .get) {  (data) in
            guard let result = (data["result"] as? [Any]?) else { return  }
            if self.pageNum == 1 {
                self.dataSource.removeAll()
                self.table.mj_header?.endRefreshing()
            }
            
            let arr = Array<TestRecordModel>.deserialize(from: result)
            if arr?.count == 0 {
                self.table.mj_footer?.endRefreshingWithNoMoreData()
            }else{
                self.table.mj_footer?.endRefreshing()
            }
            self.dataSource += arr ?? []
            self.table.reloadData()
        } _: {  (error) in
            SVProgressHUD.showError(error)
        }
    }
}
